package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.GoodsComment;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.service.GoodsCommentService;
import cn.duli.xianer.service.WantGoodsCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goodsComment")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService commentService;

    @Autowired
    private WantGoodsCommentService wantGoodsCommentService;

    /**
     * 根据商品id查询该商品下的所有评论
     * @param gId
     * @return
     */
    @GetMapping("/findByGoodId/{gId}")
    public MyResult findByGoodId(@PathVariable("gId") Integer gId) {
        List<GoodsComment> allComments = commentService.findByGoodId(gId);
        if (allComments == null || allComments.size() == 0) {
            return new MyResult(200, "根据商品id查询该商品下的所有评论为空",null,null,null,null);
        }
        List<GoodsComment> comments = new ArrayList<>();
        List<GoodsComment> parents = new ArrayList<>();
        for (GoodsComment comment : allComments) {
            if (comment.getGcParentid()==null) {
                comments.add(comment);
                parents.add(comment);
            } else {
                boolean foundParent=false;
                for (GoodsComment parent : parents) {
                    if (comment.getGcParentid() == parent.getGcId()) {
                        if (parent.getChild() == null) {
                            parent.setChild(new ArrayList<>());
                        }
                        parent.getChild().add(comment);
                        parents.add(comment);
                        foundParent=true;
                        //如果对list迭代过程中同时修改list，会报java.util.ConcurrentModificationException
                        // 的异常，所以我们需要break,当然break也可以提高算法效率
                        break;
                    }
                }
                if (!foundParent) {
                    throw new RuntimeException("can not find the parent comment");
                }
            }
        }
        return new MyResult(200, "根据商品id查询该商品下的所有评论",null,comments,null,null);
    }

    /**
     * 新增评论
     * @param goodsComment
     * @return
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult save(GoodsComment goodsComment) {
        commentService.save(goodsComment);
        return  new MyResult(200, "新增评论",null,null,null,null);
    }

    /**
     * 根据用户id查询该用户发布的所有评论
     * @param uId
     * @return
     */
    @GetMapping("/findByUserId/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business", "admin"})
    public MyResult findByUserId(@PathVariable("uId") Integer uId,
                                 @RequestParam(defaultValue = "1",required = false) int pageNo,
                                 @RequestParam(defaultValue = "6",required = false) int pageSize ) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<GoodsComment> pageInfo = new PageInfo<>(commentService.findByUserId(uId));
        return  new MyResult(200, "根据用户id查询该用户发布的所有评论",null,null,null,pageInfo);
    }

    /**
     * 根据评论id删除此评论(如果该评论是父评论，删除此父评论的同时，也会删除相关的子评论哦)
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business", "admin"})
    public MyResult deleteById(@PathVariable("id") Integer id) {
        commentService.deleteById(id);
        List<GoodsComment> list = commentService.findByParentId(id);
        if (list.size() > 0) {
            for (int i=0;i<list.size();i++) {
                commentService.deleteByParentId(list.get(i).getGcParentid());
            }
        }
        return  new MyResult(200, "根据评论id删除此评论",null,null,null,null);
    }

    /**
     * 分页查找所有评论
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                            @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<GoodsComment> pageInfo = new PageInfo<>(commentService.findAll());
        return  new MyResult(200, "分页查找所有评论",null,null,null,pageInfo);
    }

    /**
     * 查找商品评论和求购评论总数
     * @return
     */
    @RequestMapping("/findAllCount")
    @RequiresRoles("admin")
    public MyResult findAllCount() {
        int gc_count = commentService.findAllCount();
        int wgc_count = wantGoodsCommentService.findAllCount();
        int count = gc_count + wgc_count;
        return new MyResult(200, "查找商品评论和求购评论总数成功",null,null,count,null);
    }

    /**
     * 根据商品id查找商品评论条数
     * @param gId
     * @return
     */
    @RequestMapping("/findCountsByGid/{gId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business", "admin"})
    public MyResult findCountsByGid(@PathVariable("gId") Integer gId) {
        int count = commentService.findCountsByGid(gId);
        return new MyResult(200, "根据商品id查找商品评论条数",null,null,count,null);
    }
}
